package org.dtrd.modules.evaluation.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wechat.pay.contrib.apache.httpclient.cert.CertificatesManager;
import org.checkerframework.checker.nullness.Opt;
import org.dtrd.modules.evaluation.entity.bean.JobInfo;
import org.dtrd.modules.evaluation.entity.po.DtrdEntEvaluationRdJob;
import org.dtrd.modules.evaluation.mapper.DtrdEntEvaluationRdJobMapper;
import org.dtrd.modules.evaluation.service.IDtrdEntEvaluationRdJobService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.StringJoiner;

/**
 * <p>
 * 马氏工作倦怠评估 服务实现类
 * </p>
 *
 * @author QR
 * @since 2021-09-26
 */
@Service
@DS("multi-datasource1")
public class DtrdEntEvaluationRdJobServiceImpl extends ServiceImpl<DtrdEntEvaluationRdJobMapper, DtrdEntEvaluationRdJob> implements IDtrdEntEvaluationRdJobService {

    @Override
    public JobInfo saveJobInfo(JobInfo jobInfo) {
        List<Integer> answerArr = jobInfo.getInput();
        int tmpScore = 0;
        int emotion = 0;
        int attitude = 0;
        int efficacy = 0;
        StringJoiner joiner = new StringJoiner(",");
        for (int i = 0; i < answerArr.size(); i++) {
            // 上传数据1-7 ，实际计分应为0-6 肖 2022-07-07 10:48:26
            tmpScore = answerArr.get(i) - 1;
            joiner.add(tmpScore + "");
            if (i < 5) {
                emotion += tmpScore;
            } else if (i >= 9) {
                tmpScore = 6 - tmpScore;
                efficacy += tmpScore;
            } else {
                attitude += tmpScore;
            }
        }
        String emotionLevel = "";
        String attitudeLevel = "";
        String efficacyLevel = "";
        StringJoiner stringJoiner = new StringJoiner("", "您", "");
        // 计分规则  总分除以15 再乘上20
        float score = (emotion + attitude + efficacy) / 3.0f * 4.0f;
        if (score < 50) {
            stringJoiner.add("工作状态良好；");
        } else if (score < 75) {
            stringJoiner.add("存在一定程度的职业倦怠；");
        } else if (score < 100) {
            stringJoiner.add("职业倦怠比较严重；");
        } else {
            stringJoiner.add("职业倦怠非常严重；");
        }

        //情绪衰竭
        if (emotion < 15) {
            emotionLevel = "低";
            stringJoiner.add("情绪衰竭维度得分较低，");
        } else if (emotion <= 30) {
            emotionLevel = "高";
            stringJoiner.add("情绪衰竭维度得分较高，");
        }

        //玩世不恭
        if (attitude < 10) {
            attitudeLevel = "低";
            stringJoiner.add("玩世不恭维度得分较低，");
        } else if (attitude <= 24) {
            attitudeLevel = "高";
            stringJoiner.add("玩世不恭维度得分较高，");
        }

        //职业效能
        if (efficacy < 23) {
            efficacyLevel = "高";
            stringJoiner.add("职业效能维度得分较高。");
        } else if (efficacy <= 36) {
            efficacyLevel = "低";
            stringJoiner.add("职业效能维度得分较低。");
        }
        DtrdEntEvaluationRdJob job = new DtrdEntEvaluationRdJob();
        if (jobInfo.getDataId() == null) {
            job = job.init();
        }
        job.setDataId(jobInfo.getDataId());
        job.setInput(joiner.toString());
        job.setEmotion(emotion);
        job.setAttitude(attitude);
        job.setEfficacy(efficacy);
        job.setEmotionLevel(emotionLevel);
        job.setAttitudeLevel(attitudeLevel);
        job.setEfficacyLevel(efficacyLevel);
        job.setOutput(stringJoiner.toString());
        if (jobInfo.getPatientId() != null) {
            job.setPatientId(jobInfo.getPatientId());
            save(job);
        }
        return new JobInfo().parseFromPo(job);
    }


    @Override
    public JobInfo getJobInfoLatest(Integer patientId) {
        List<DtrdEntEvaluationRdJob> list = getJobEvaluationList(patientId, true);
        return Optional.ofNullable(list).map(data -> {
            JobInfo jobInfo = new JobInfo();
            return jobInfo.parseFromPo(data.get(0));
        }).orElse(new JobInfo());
    }

    @Override
    public List<DtrdEntEvaluationRdJob> getJopEvaluationList(Integer patientId) {
        return getJobEvaluationList(patientId, false);
    }

    private List<DtrdEntEvaluationRdJob> getJobEvaluationList(Integer patientId, boolean limit1) {
        LambdaQueryWrapper<DtrdEntEvaluationRdJob> queryWrapper = Wrappers.lambdaQuery(DtrdEntEvaluationRdJob.class)
                .eq(DtrdEntEvaluationRdJob::getIsDel, 0)
                .eq(DtrdEntEvaluationRdJob::getPatientId, patientId)
                .orderByDesc(DtrdEntEvaluationRdJob::getCreateTime);
        if (limit1) {
            queryWrapper.last("limit 1");
            DtrdEntEvaluationRdJob po = getOne(queryWrapper);
            return Optional.ofNullable(po).map(value -> {
                List<DtrdEntEvaluationRdJob> list = new ArrayList<>(1);
                list.add(value);
                return list;
            }).orElse(null);
        } else {
            return list(queryWrapper);
        }
    }

}
